<template>
  <div class="sys-role">
  <el-dialog
    direction="ltr"
    :show-close="false"
    :visible.sync="visible"
    :before-close="closeModal">
    <el-card class="box-card">
      <div slot="header" >
        <span>配置用戶角色</span>
        <el-button size="small" style="float: right" type="text" @click="cancelSelected">取消选中</el-button>
      </div>
    <el-table
      size="small"
      :data="deptTree"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      ref="treeTable"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'isChild'}">
      <el-table-column  prop="name" label="部门名称"  width="50" ></el-table-column>
      <el-table-column   label="角色名称"  width="180" >
        <template slot-scope="scope">
          <el-radio-group v-model="roleId" size="small" >
            <el-radio   v-for="item in getChild(scope.row.id)" :label="item.id" :key="item.id" :disabled="checkDept(item.deptId)">{{item.name}}</el-radio>
          </el-radio-group>
        </template>
      </el-table-column>
    </el-table>
    </el-card>
    <div  slot="footer" style="margin-right: 30px">
      <el-button @click="closeModal" size="small">取 消</el-button>
      <el-button v-loading="loading" type="primary" @click="submitSysRole"  size="small">确 定</el-button>
    </div>
  </el-dialog>
  </div>
</template>

<script>
import util from '@/libs/util'
import { getUserRole, bandUserRole } from '@/api/system/user'
export default {
  name: 'sys-role',
  props: {
    // eslint-disable-next-line vue/require-valid-default-prop
    roles: { type: Array, default: [] },
    // eslint-disable-next-line vue/require-valid-default-prop
    deptTree: { type: Array, default: [] }
  },
  data () {
    return {
      visible: false,
      userId: '',
      loading: false,
      roleId: '',
      deptId: ''
    }
  },
  watch: {
  },
  created () {
  },
  methods: {
    closeModal () {
      this.resetModel()
      this.visible = false
    },
    resetModel () {
      this.roleId = ''
      this.loading = false
      this.userId = ''
    },
    getChild (pid) {
      const role = []
      for (const t of this.roles) {
        if (util.eq(t.deptId, pid)) {
          role.push(t)
        }
      }
      return role
    },
    submitSysRole () {
      const param = { userId: this.userId, roleId: this.roleId }
      util.request(this, bandUserRole, param, undefined, '绑定角色成功')
    },
    selectChange (e) {
    },
    getUserRoleInfo (userId, deptId) {
      this.deptId = deptId && deptId
      getUserRole({ userId: userId }).then(res => {
        this.roleId = res && res.id
      })
    },
    checkDept (deptId) {
      return !util.eq(deptId, this.deptId)
    },
    cancelSelected () {
      this.roleId = ''
    }
  }
}
</script>

<style scoped>
.sys-role >>> .el-dialog__header{
  padding: 0;
}
</style>
